import * as types from './mutation-types'
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {saveSearch, clearSearch, deleteSearch, savePlay, saveFavorite, deleteFavorite} from 'common/js/cache'

function findIndex(list, song) { //查找当前列表中 是否有这一条数据
  return list.findIndex((item) => {
    return item.id === song.id
  })
}

//选择播放 music-list
export const selectPlay = function ({commit, state}, {list, index}) {
// 	let sequenceList = state.sequenceList.slice() //顺序列表 2019 1 10
// 	console.log("sequenceList",sequenceList)
// 	let obj={data:list,index}
// 	let oj={mun:sequenceList}
//  console.log('inx',{song,inx})
//  const dta = Object.assign({}, obj, oj)  //播放列表数据的不统一
//  console.log("dtadta",dta)
  // commit(types.SET_SEQUENCE_LIST, dta)//顺序列表
	 // commit(types.SET_SEQUENCE_LIST, {list,index})//顺序列表
	 
	let obj={data:list,index}
	// let sp=[...state.sequenceList]
	// console.log("spp",sp)
	 const dta = Object.assign({}, obj, {mun:list})  //播放列表数据的不统一
	 
	console.log("objpp",dta)
	commit(types.SET_SEQUENCE_LIST, dta)//顺序列表
  if (state.mode === playMode.random) {
    let randomList = shuffle(list)
    commit(types.SET_PLAYLIST, randomList)//播放状态
    index = findIndex(randomList, list[index])
  } else {
    commit(types.SET_PLAYLIST, list)//播发列表
  }
  commit(types.SET_CURRENT_INDEX, index)//播放状态
  commit(types.SET_FULL_SCREEN, true)//播放器张开还是收起
  commit(types.SET_PLAYING_STATE, true)//播放状态
}

export const randomPlay = function ({commit,state}, {list}) {//随机播发黄色的文字
console.log("pkkkkkkk")
	commit(types.SET_CURRENT_INDEX, 0)//播放index
	let currentIndex =JSON.parse(JSON.stringify(state.currentIndex))
	let obj={data:list,index:currentIndex}
	const dta = Object.assign({}, obj, {mun:list})//播放列表数据的不统一
  commit(types.SET_PLAY_MODE, playMode.random) //播放模式
  commit(types.SET_SEQUENCE_LIST,dta)//顺序列表
  let randomList = shuffle(list)
	commit(types.SET_PLAYLIST, randomList)//播发列表
  // commit(types.SET_CURRENT_INDEX, 0)//播放状态
  commit(types.SET_FULL_SCREEN, true)//播放器张开还是收起
  commit(types.SET_PLAYING_STATE, true)//播放状态
}

//排行榜点击选择音乐
export const insertSong = function ({commit, state},song) {
	let pu=[]
	let pk=[]
			pu=song.data[song.index]
			console.log("playlist",state.playlist)
			console.log("sequenceList",state.sequenceList)
			
  let playlist = [...state.playlist] //播发列表
	let spm= [...state.sequenceList] //顺序列表
	let sequenceList=spm.length>0?spm.length:pk
	 
  let currentIndex =[...state.currentIndex]//当前播放的索引
  // 记录当前歌曲
  let currentSong = playlist[currentIndex]
  console.log("song.index",song.index)
  console.log("currentIndex",currentIndex)
	console.log("currentSong",currentSong)
  // 查找当前列表中是否有待插入的歌曲并返回其索引
  // let fpIndex = findIndex(playlist, song)
	let fpIndex = findIndex(playlist, pu)
  // 因为是插入歌曲，所以索引+1
  currentIndex++
  // 插入这首歌到当前索引位置
  playlist.splice(currentIndex, 0, pu)
  // 如果已经包含了这首歌
  if (fpIndex > -1) {
    // 如果当前插入的序号大于列表中的序号
    if (currentIndex > fpIndex) {
      playlist.splice(fpIndex, 1)
      currentIndex--
    } else {
      playlist.splice(fpIndex + 1, 1)
    }
  }
//要插入的位子
  let currentSIndex = findIndex(sequenceList, currentSong) + 1

  let fsIndex = findIndex(sequenceList, pu)

  sequenceList.splice(currentSIndex, 0, pu)

  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {//添加在后面
      sequenceList.splice(fsIndex, 1)
    } else { //添加在前面
      sequenceList.splice(fsIndex + 1, 1)
    }
  }
 	let oj={mun:sequenceList}
  // console.log('inx',{song,inx})
	const dta = Object.assign({}, song, oj)  //播放列表数据的不统一
	console.log(dta)
  commit(types.SET_PLAYLIST, playlist) //播发列表
  // commit(types.SET_SEQUENCE_LIST,song) //顺序列表
	commit(types.SET_SEQUENCE_LIST,dta) //顺序列表
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULL_SCREEN, true) //播放器张开还是收起
  commit(types.SET_PLAYING_STATE, true)//播放状态
}

//搜索历史
export const saveSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearch(query))
}
//删除的通用方法
export const deleteSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, deleteSearch(query))
}
//删除方法
export const clearSearchHistory = function ({commit}) {
  commit(types.SET_SEARCH_HISTORY, clearSearch())
}

//删除歌曲
export const deleteSong = function ({commit, state}, song) {
	
  let playlist = [...state.playlist]
  let sequenceList = [...state.sequenceList]
  let currentIndex =JSON.parse(JSON.stringify(state.currentIndex))
	
  let pIndex = findIndex(playlist, song)
  playlist.splice(pIndex, 1)
  let sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)
	
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }

  commit(types.SET_PLAYLIST, playlist)
	let obj={data:playlist,index:currentIndex}
	const dta = Object.assign({}, obj, {mun:sequenceList})//播放列表数据的不统一
	
	commit(types.SET_SEQUENCE_LIST,dta)//顺序列表
	
  // commit(types.SET_SEQUENCE_LIST, sequenceList)
	
  commit(types.SET_CURRENT_INDEX, currentIndex)

//   if (!playlist.length) { 2019 1 16 精简
//     commit(types.SET_PLAYING_STATE, false)
//   } else {
//     commit(types.SET_PLAYING_STATE, true)
//   }
    
		const playingState=playlist.length>0
		commit(types.SET_PLAYING_STATE, playingState)
}

//playlist  全部删除
export const deleteSongList = function ({commit}) {
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_CLEAR, [])
  commit(types.SET_PLAYING_STATE, false)
}

//播放器 写入缓冲
export const savePlayHistory = function ({commit}, song) {
  commit(types.SET_PLAY_HISTORY, savePlay(song))
}

//用户喜欢的数据 add
export const saveFavoriteList = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, saveFavorite(song))
}
//用户喜欢的数据 clear
export const deleteFavoriteList = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
}
